//
//  MeTableHeaderView.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/6.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "MeTableHeaderView.h"

#import "BaseViewController.h"

#import "MeInformationEditVC.h"

@interface MeTableHeaderView ()

@property (weak, nonatomic) IBOutlet UIView *iconBgView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nickNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *leftMarkLabel;
@property (weak, nonatomic) IBOutlet UILabel *middleMarkLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightMarkLabel;
@property (weak, nonatomic) IBOutlet UIButton *storeBtn;
@property (weak, nonatomic) IBOutlet UIButton *footBtn;
@property (weak, nonatomic) IBOutlet UIView *topBgView;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UIImageView *userEditImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nickLabelBottomMargin;

@end

@implementation MeTableHeaderView


- (void)awakeFromNib{
    
    [super awakeFromNib];
    
    self.rightBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.rightBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;

    
    self.topBgView.backgroundColor = themeColor;
    
    self.backgroundColor = themeColor;
    
    self.iconBgView.layer.cornerRadius = 32;
    
    self.iconImageView.layer.cornerRadius = 30;
    
    self.iconImageView.layer.masksToBounds = YES;
    
    self.nickNameLabel.font = MYFONT(17);
    
    self.leftMarkLabel.font = MYFONT(14);
    
    self.middleMarkLabel.font = MYFONT(14);
    
    self.rightMarkLabel.font = MYFONT(14);
    
    self.storeBtn.titleLabel.font = MYFONT(15);
    
    self.footBtn.titleLabel.font = MYFONT(15);
    
    if (myViewModel.userIsLogin) {
        
        self.leftMarkLabel.hidden = NO;
        
        self.leftMarkLabel.text = myViewModel.user.user_tel;
        
        self.userEditImageView.hidden = NO;
        
        self.nickNameLabel.text = myViewModel.user.nick_name;

        self.nickLabelBottomMargin.constant = -3;
        
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:myViewModel.user.user_headimg] placeholderImage:[UIImage defaulIconImage]];

    } else {
        
        self.leftMarkLabel.hidden = YES;
        
        self.userEditImageView.hidden = YES;
        
        self.nickNameLabel.text = @"点击登录";
        
        self.nickLabelBottomMargin.constant = 10;
        
        self.iconImageView.image = [UIImage defaulIconImage];

    }
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userDidLoginOrInfoChange) name:@"userDidLoginOrInfoChange" object:nil];
    
}

- (void)userDidLoginOrInfoChange{
    
    if (myViewModel.userIsLogin) {
        
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:myViewModel.user.user_headimg] placeholderImage:[UIImage defaulIconImage]];

        
        self.leftMarkLabel.hidden = NO;
        
        self.leftMarkLabel.text = myViewModel.user.user_tel;
        
        self.userEditImageView.hidden = NO;
        
        self.nickNameLabel.text = myViewModel.user.nick_name;
        
        self.nickLabelBottomMargin.constant = -3;
        
    } else {
        
        self.leftMarkLabel.hidden = YES;
        
        self.userEditImageView.hidden = YES;
        
        self.nickNameLabel.text = @"点击登录";
        
        self.nickLabelBottomMargin.constant = 10;
        
        
        self.iconImageView.image = [UIImage defaulIconImage];

    }
    
    
    
    
    
}

- (void)dealloc{
    
    
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    
}



- (IBAction)bigBtnDidClick:(UIButton *)sender {
    
    if (myViewModel.userIsLogin) {
    
        MeInformationEditVC *temp = [[MeInformationEditVC alloc]init];
        
        [self.viewController.navigationController pushViewController:temp animated:YES];
        
    } else {

        [myViewModel.rootController login];
    }

  
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {

 
 
 }
*/

@end
